/**
 * Copyright &copy; 2012-2016 <a href="https://www.kaaction.com">KAA</a> All rights reserved All rights reserved.
 */
package com.kaaction.modules.crab.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.kaaction.common.config.Global;
import com.kaaction.common.persistence.Page;
import com.kaaction.common.web.BaseController;
import com.kaaction.common.utils.DateUtils;
import com.kaaction.common.utils.StringUtils;
import com.kaaction.common.utils.excel.ExportExcel;
import com.kaaction.modules.crab.entity.MsgHistroy;
import com.kaaction.modules.crab.entity.Order;
import com.kaaction.modules.crab.service.MsgHistroyService;
import com.kaaction.modules.crab.service.OrderService;
import com.kaaction.modules.crab.utils.SingleSendSms;

/**
 * 短信历史Controller
 * @author huanghaiming
 * @version 2017-07-24
 */
@Controller
@RequestMapping(value = "${adminPath}/crab/msgHistroy")
public class MsgHistroyController extends BaseController {

	@Autowired
	private MsgHistroyService msgHistroyService;
	
	@Autowired
	private OrderService orderService;
	
	@ModelAttribute
	public MsgHistroy get(@RequestParam(required=false) String id) {
		MsgHistroy entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = msgHistroyService.get(id);
		}
		if (entity == null){
			entity = new MsgHistroy();
		}
		return entity;
	}
	
	@RequiresPermissions("crab:msgHistroy:view")
	@RequestMapping(value = {"list", ""})
	public String list(MsgHistroy msgHistroy, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<MsgHistroy> page = msgHistroyService.findPage(new Page<MsgHistroy>(request, response), msgHistroy); 
		model.addAttribute("page", page);
		return "modules/crab/msgHistroyList";
	}

	@RequiresPermissions("crab:msgHistroy:form")
	@RequestMapping(value = "form")
	public String form(MsgHistroy msgHistroy, Model model) {
		model.addAttribute("msgHistroy", msgHistroy);
		return "modules/crab/msgHistroyForm";
	}

	@RequiresPermissions("crab:msgHistroy:save")
	@RequestMapping(value = "save")
	public String save(MsgHistroy msgHistroy, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, msgHistroy)){
			return form(msgHistroy, model);
		}
		msgHistroyService.save(msgHistroy);
		addMessage(redirectAttributes, "保存短信历史成功");
		return "redirect:"+Global.getAdminPath()+"/crab/msgHistroy/?repage";
	}
	
	@RequiresPermissions("crab:msgHistroy:delete")
	@RequestMapping(value = "delete")
	public String delete(MsgHistroy msgHistroy, RedirectAttributes redirectAttributes) {
		msgHistroyService.delete(msgHistroy);
		addMessage(redirectAttributes, "删除短信历史成功");
		return "redirect:"+Global.getAdminPath()+"/crab/msgHistroy/?repage";
	}
	
	/**
	 * 导出短信历史数据
	 * @param user
	 * @param request
	 * @param response
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("crab:msgHistroy:export")
    @RequestMapping(value = "export", method=RequestMethod.POST)
    public String exportFile(MsgHistroy msgHistroy, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
            String fileName = "短信历史数据"+DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
            List<MsgHistroy> list = msgHistroyService.findMsgHistroy(msgHistroy);
    		new ExportExcel("短信历史数据", MsgHistroy.class).setDataList(list).write(response, fileName).dispose();
    		return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导出短信历史数据失败！失败信息："+e.getMessage());
		}
		return "redirect:"+Global.getAdminPath()+"/crab/msgHistroy/?repage";
    }
	
	@RequiresPermissions("crab:msgHistroy:pushMessage")
	@RequestMapping(value = "pushMessage")
	public String pushMessage(MsgHistroy msgHistroy, RedirectAttributes redirectAttributes) {
		String orderId = msgHistroy.getOrderId();
		if(orderId!=null&&!"".equals(orderId)){
			Order order = new Order();
			order.setId(orderId);
			order = orderService.get(order);
			SingleSendSms app = new SingleSendSms();
			String str = app.sendMsg(msgHistroy.getMobile(), "{'orderNo':'"+order.getOrderNum()+"','expresNo':'"+order.getExpressNum()+"'}");
			if("true".equals(str)){
				 order.setMsgState("1");
				 msgHistroy.setState("0");
				 msgHistroyService.update(msgHistroy);
				 orderService.updateState(order);
				 addMessage(redirectAttributes, "信息发送成功");
			 }else{
				 addMessage(redirectAttributes, "信息发送失败");
			 }
		}else{
			 addMessage(redirectAttributes, "信息发送失败");
		}
		return "redirect:"+Global.getAdminPath()+"/crab/msgHistroy/?repage";
	}

}